<?php
	namespace app\service\admin;
	use core\BaseService;
	use app\model\Label;
	use core\Util;
	use core\App;
	use app\model\ArticleLabel;
	class LabelService extends BaseService{
		function model(){
			return Label::$tableName;
		}
		
		function saveLabel($params,$info){
			$user = App::getCurrentUser();
			if($params['id']){
				$params = Label::filter($params);
				$params['user_id'] = $user->id;
				$params['user_name'] = $user->login_name;
				$this->update($params);
				$info->success =true;
				$info->msg = '操作成功，数据已更新！';
			}else{
				$params = Label::filter($params);
				$time = Util::time();
				$params["create_time"] = $time;
				$params['user_id'] = $user->id;
				$params['user_name'] = $user->login_name;
				$this->insert($params);
				$info->success =true;
				$info->msg = '操作成功，数据已保存！';
			}
		}
		
		function getLabelsByArticleId($ids){
			$mTable = ArticleLabel::$tableName;
			return $this->table($this->model())
				->join($mTable,
					$this->model().'.id', '=', 
					$mTable.'.label_id')
				->whereIn($mTable.'.article_id',explode(',',$ids))
				->select($this->model().'.*')
				->get();
		}
		
		function deleteById($ids,$info){
			$idArr = explode(',',$ids);
			//删除标签
			$count = $this->table()->whereIn('id',$idArr)->delete();
			//删除文章的标签
			$this->table(ArticleLabel::$tableName)->whereIn('label_id',$idArr)->delete();
			$info->success = true;
			$info->msg = "操作成功，已删除".$count."条数据！";
		}
	}